<?php


namespace app\admin\controller;


use app\admin\validate\AdminUser as AdminValidate;
use think\Exception;
use think\facade\View;

class Login extends AdminBase
{

    public function initialize()
    {
        if ($this->isLogin()){
            return $this->redirect(url("index/index"));
        }
    }

    public function index(){
        return View::fetch();
    }

    public function md5(){
        dump(session(config('admin.session_admin')));
//        halt(session(config('admin.session_admin')));
//        echo md5('123456');
    }

    public function check(){
        if (!$this->request->isPost()) {
            return show(config('status.error'), '请求方式错误');
        }
        $username = $this->request->param("username", "", "trim");
        $password = $this->request->param("password", "", "trim");
        $captcha = $this->request->param("captcha", "", "trim");
        $validate = new AdminValidate();
        $data = [
            'username' => $username,
            'password' => $password,
            'captcha' => $captcha
        ];
        if (!$validate->check($data)){
            return show(config('status.error'), $validate->getError());
        }
        if (empty($username) || empty($password) || empty($captcha)) {
            return show(config('status.error'), '参数不能为空');
        }
        try {
            $result = \app\admin\business\AdminUser::login($data);
        }catch (Exception $e){
            return show(config('status.error'), $e->getMessage());
        }
        if ($result) {
            return show(config('status.success'), '登录成功');
        }else{
            return show(config('status.error'), '登录失败');
        }
    }
}